//
//  DemoNavBarVC.swift
//  BWUIKit_Example
//
//  Created by zhuxuhong on 2022/2/18.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import BWUIKit
import BWListKit

class DemoNavBarVC: NavigationBarController {
    override func didLoad() {
        super.didLoad()
        
        navBarTintColor = .blue
        navLeftBtns = [
            .init(barButtonSystemItem: .cancel, target: self, action: #selector(navBack(_:)))
        ]
        navBarBackgroundColor = .red
        
        listAdapter.scrollDelegate = self
        makeListData()
    }
    
    func makeListData() {
        let items: [BWListItem] = (0..<100).map{
            .init(reuseId: Cell.RID, height: 64, data: $0)
        }
        listAdapter.data = .init(registers: [
            .init(style: .cell, class: Cell.self)
        ], sections: [
            .init(items: items)
        ])
    }
    
    @objc private func navBack(_ sender: UIBarButtonItem) {
        navigationController?.popViewController(animated: true)
    }
}



extension DemoNavBarVC: BWListScrollDelegate {
    public func bwListScrollViewDidScroll(_ scrollView: UIScrollView) {
        let y = scrollView.contentOffset.y
        let maxH: CGFloat = 88
        
        let alpha = max(0, min(1, y/maxH))
        let isTranslucent = alpha<0.7
        let tintColor = isTranslucent ? UIColor.black : UIColor.white
        let barColor = UIColor.blue.withAlphaComponent(alpha)
        navBarTintColor = tintColor
        navBarBackgroundColor = barColor
    }
}
